<template>
    <el-dialog
        :before-close="resetForm"
        :close-on-click-modal="false"
        :visible.sync="visible"
        append-to-body
        title="编辑租户信息"
        width="560px"
    >
        <div class="edit_page_div">
            <div>
                <el-form ref="form" :model="form" :rules="rules" :show-message="false" label-width="100px" style="float: left;">
                    <el-form-item label="编号" prop="tenantCode">
                        <el-input v-model="form.tenantCode" style="width: 400px;" placeholder="系统生成" disabled></el-input>
                    </el-form-item>
                    <el-form-item label="名称" prop="tenantName">
                        <el-input v-model="form.tenantName" style="width: 400px;"></el-input>
                    </el-form-item>
                    <el-form-item label="账号" prop="accountNumber">
                        <el-input v-model="form.accountNumber" style="width: 400px;"></el-input>
                    </el-form-item>
                    <el-form-item label="密码" prop="password" v-if="!form.id">
                        <el-input v-model="form.password" style="width: 400px;"></el-input>
                    </el-form-item>
                </el-form>
            </div>
            <div class="edit_button" style="margin-top: 10px">
                <el-button v-loading.fullscreen.lock="loading" type="primary" @click="submit">保 存</el-button>
                <el-button type="primary" @click="resetForm">取 消</el-button>
            </div>
        </div>
    </el-dialog>
</template>

<script>
import SelectOption from "@/components/select/selectOption.vue";

export default {
    name: "tenantEdit",
    components: {SelectOption},
    props: {
        visible: Boolean,
        id: String,
    },
    data() {
        return {
            loading: false,
            showUser: false,
            form: {
                tenantCode: '', //会员编号
                tenantName: '', //会员名称
                accountNumber: '', //金额
                password: '', //折扣
            },
            rules: {
                tenantName: [{required: true, message: '', trigger: 'change'}],
                accountNumber: [{required: true, message: '', trigger: 'change'}],
                password: [{required: true, message: '', trigger: 'change'}],
            },
        }
    },
    watch: {
        visible(newVal) {
            if (newVal) {
                if (this.id) {
                    this.getData();
                }
            }
        }
    },
    methods: {
        getData() {
            this.loading = true;
            this.$axios.get("/CHILD_SYSTEM/systemTenant/getOneEntity/" + this.id).then(res => {
                this.form = res.data;
            }).finally(() => {
                this.loading = false;
            })
        },
        submit() {
            this.$refs.form.validate((valid) => {
                if (valid) {
                    this.loading = true;
                    this.$axios.post("/CHILD_SYSTEM/systemTenant/" + (this.id ? "update" : "insert"), this.form).then(res => {
                        this.resetForm();
                        this.$message.success(res.message);
                    }).finally(() => {
                        this.loading = false;
                    })
                } else {
                    this.$message.error('表单填写有误');
                    return false;
                }
            });
        },
        resetForm() {
            this.$refs.form.resetFields();
            this.form = this.$options.data().form;
            this.$emit('update:visible', false);
            this.$emit('refreshList');
        },
    }
}
</script>

<style scoped>

</style>